package com.inspur.gs.sbssdl.sbssgl.sbtz.sbtz.back.bizactions;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.core.LcpUtil;
import com.inspur.edp.cef.entity.changeset.*;
import com.inspur.edp.bef.api.be.*;
import com.inspur.edp.bef.api.action.VoidActionResult;
import com.inspur.edp.bef.spi.action.AbstractManagerAction;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.svc.document.storage.entity.GspDocMetadata;
import com.inspur.gs.sbssdl.sbssgl.sbtz.sbtz.back.sbsssbtz.common.QrMetaData;
import com.inspur.gs.sbssdl.sbssgl.sbtz.sbtz.back.sbsssbtz.common.SbssSbtzQrImageUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;


public class SbssSbtzreflashQRMgrAction extends AbstractManagerAction<VoidActionResult> {
	private ArrayList<String> id;
	public SbssSbtzreflashQRMgrAction(IBEManagerContext managerContext, ArrayList<String> id) {
		super(managerContext);
		this.id = id;
	}
	private String sbtzCfg = "com.inspur.gs.sbssdl.sbssgl.sbtz.sbtz.back.SbssSbtz";

	@Override
	public  void execute() {
		IStandardLcp tzLcp = LcpUtil.getLcp(sbtzCfg);

		HashMap<String, IEntityData> datas = tzLcp.retrieve(id).getDatas();
		ArrayList<String> ids = new ArrayList<>(datas.keySet());

		ids.parallelStream().forEach( devId -> {
			IEntityData data = datas.get(devId);

			String sbbh = data.getValue("sbbh").toString();

			ModifyChangeDetail modifyChangeDetail = new ModifyChangeDetail(devId);

			SbssSbtzQrImageUtil sbtzQrImageUtil = new SbssSbtzQrImageUtil();
			QrMetaData qrMetaData = sbtzQrImageUtil.updQRCode(sbbh);

			GspDocMetadata metadata = qrMetaData.getMetadata();
			String attId = qrMetaData.getAttachmentId();

			HashMap<String, Object> map = new HashMap<>();
			map.put("AttachmentId", attId);
			map.put("FileName", metadata.getFileName() + ".png" );
			map.put("FileSize", new BigDecimal(metadata.getDocSize()));
			map.put("FileCreateTime", new Date());
			modifyChangeDetail.putMultiUdtChange("qrcode", map);

			tzLcp.modify(modifyChangeDetail);
		});
		tzLcp.save();
	}
	private IBusinessEntity getEntity(String dataId) {
		return (IBusinessEntity)super.getBEManagerContext().getEntity(dataId);
	}	private IBEService getMgr() {
		return (IBEService)super.getBEManagerContext().getBEManager();
	}
}